/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
  * Name: mp_log.h
  *
  * Purpose: log trace API define for micro player
  *
  * Developer:
  *   wen.gu , 2018-09-03
  *
  * TODO:
  *
  ***************************************************************************/


#ifndef __MP_LOG_H__
#define __MP_LOG_H__

/******************************************************************************
**    INCLUDES
******************************************************************************/

#include <string>
#include <stdint.h>
#include <functional>

#include "mp_types.h"

/******************************************************************************
**    MACROS
******************************************************************************/


/** log macro */
#ifndef LOG_TAG
#define LOG_TAG "none" 
#endif

#ifndef LOGE 
#define LOGE(param, ...) MFLOG(MPL_LevelError, param, ##__VA_ARGS__)
#endif

#ifndef LOGI
#define LOGI(param, ...) MFLOG(MPL_LevelInfo, param, ##__VA_ARGS__)
#endif

#ifndef LOGD
#define LOGD(param, ...) MFLOG(MPL_LevelDebug, param, ##__VA_ARGS__)
#endif

#ifndef LOGW
#define LOGW(param, ...) MFLOG(MPL_LevelWarning, param, ##__VA_ARGS__)
#endif

#ifndef MFLOG
/**%s[%s][%s][%s][%s.%d]: timestamp, log type, module, sub module, func, line */
/** type: short string for debug level */
#define MFLOG(level, param, ...) \
        MPLog::getInstance().logPrint(level, LOG_TAG, __FUNCTION__,\
                        __LINE__, param, ##__VA_ARGS__)

#endif

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

/** this is binding member function of class */
#define MAKE_LOGFUNC(method, obj) \
    std::bind(&method, obj, std::placeholders::_1, std::placeholders::_2, \
             std::placeholders::_3, std::placeholders::_4, \
             std::placeholders::_5)

/** this is binding globle non member function */
#define MAKE_LOGFUNC2(method) \
        std::bind(&method, std::placeholders::_1, std::placeholders::_2, \
             std::placeholders::_3, std::placeholders::_4, \
             std::placeholders::_5)
/******************************************************************************
**    TYPE DEFINITIONS
******************************************************************************/

typedef std::function<
    void(eMPLogLevel, /** log level */
    const char*, /** tag/context name */
    const char*, /** the name of log called function*/
    int,         /** the line number of log called */
    const char*  /** the content of log */
    )> MPLogCallbackFunc;

/******************************************************************************
**    CLASSES/FUNCTIONS DEFINITIONS
******************************************************************************/





/** media log class */
class MP_CLASS MPLog
{
protected:
    MPLog();
    ~MPLog();
public:
    void setObserver(MPLogCallbackFunc cbFunc);
    void setModuleName(const char* name);
    void setLogLevel(eMPLogLevel mll);
    
    void logPrint(   eMPLogLevel mll, const char* tag,
                      const char* func, int line, const char* fmt, ...);                   

    const char* logLevel2ShortStr(eMPLogLevel mll);
    void makeTimestampStr(char buf[], size_t len); 

    void makeLogPrefix(char buf[], size_t len, /** output buffer */
                             eMPLogLevel mll, const char* tag,
                             const char* func, int line);
public:    
    inline const char* getModuleName(){ return mModuleName.c_str(); }
    inline eMPLogLevel getLogLevel(){return mLevel;}
    
public:
    static MPLog& getInstance();

public:
    

    /** if not set observer, then this function will be called print to std */
    void defaultPrint(eMPLogLevel mll, 
                      const char* tag,
                      const char* func, 
                      int line, 
                      const char* str);
                           
                      
private:
    bool mEnableTimestamp = false;
    std::string mModuleName = "MP"; /** like "MP" */
    MPLogCallbackFunc mCallback = nullptr;
    
    /** only logPrint level <= mLevel can be log output */
    eMPLogLevel mLevel = MPL_LevelDebug; 
    char mTimestampBuf[128];
};



#endif /** !__MP_LOG_H__ */

